package com.easyfun.healthmagicbox.bluetooth.ycgi;

import java.util.Calendar;

import com.easyfun.healthmagicbox.bluetooth.BTMessage;

public class YiChengGIMessage extends BTMessage<YiChengGIMsgType>{
	static byte[] DetectCommand = null;
	static byte[] serialNumber = {0x00,0x00,0x02,(byte) 0xF1};
	byte workFlowStep;
	int bloodsugar;
	long datetime;
	
	public byte getWorkFlowStep() {
		return workFlowStep;
	}
	
	public void setWorkFlowStep(byte workFlowStep) {
		this.workFlowStep = workFlowStep;
	}
	
	
	
	public int getBloodsugar() {
		return bloodsugar;
	}

	public void setBloodsugar(byte highbloodsugar,byte lowbloodsugar) {
		this.bloodsugar = 256 * (0xFF & highbloodsugar) + (0xFF & lowbloodsugar);
	}

	public long getDatetime() {
		return datetime;
	}

	public void setDatetime(long datetime) {
		this.datetime = datetime;
	}

	protected void setDatetime(byte[] bcdDateTime) {
		if (bcdDateTime.length == 6)
		{
			Calendar cal = Calendar.getInstance();
		    cal.set(Calendar.YEAR, 2000 + bcdDateTime[0]);
		    cal.set(Calendar.MONTH, bcdDateTime[1]);
		    cal.set(Calendar.DAY_OF_MONTH, bcdDateTime[2]);
		    cal.set(Calendar.HOUR_OF_DAY, bcdDateTime[3]);
		    cal.set(Calendar.MINUTE, bcdDateTime[4]);
		    cal.set(Calendar.SECOND, bcdDateTime[5]);
			this.datetime = cal.getTimeInMillis();
		}
	}
	
	private static void fillinSerialNumber(byte[] detectCommand2) {
		detectCommand2[2] = serialNumber[0];
		detectCommand2[3] = serialNumber[1];
		if (detectCommand2.length > 6)
		{
			detectCommand2[5] = serialNumber[2];
			detectCommand2[6] = serialNumber[3];
		}
		
	}
	//4F 06 FF FF 04 FF FF 00 05 
	public static byte[] retrieveDetectCommand()
	{
		if (DetectCommand == null)
		{
			DetectCommand = new byte[]{ 0x4F,0x06,(byte) 0xFF,(byte) 0xFF,0x04,
					(byte) 0xFF,(byte) 0xFF,0x00,0x05,0x00 };
			fillinSerialNumber(DetectCommand);
			byte sumxor = 0 ;
			for (int i=0; i < DetectCommand.length - 1; i++)
			{
				if( i==0 )
				{
					sumxor = DetectCommand[0];					
				}else
				{
					sumxor = (byte) (sumxor^(DetectCommand[i]));
				}
			}
			DetectCommand[DetectCommand.length-1] = sumxor;
		}
		
		return DetectCommand;
	}

	//4F 06 00 00 03 02 F1 47 
	public static byte[] retrieveReadyCommand()
	{
		if (DetectCommand == null)
		{
			DetectCommand = new byte[]{ 0x4F,0x06,(byte) 0x00,(byte) 0x00,0x03,
					(byte) 0x02,(byte) 0xF1,(byte)0xFE,0x00 };
			fillinSerialNumber(DetectCommand);
			byte sumxor = 0 ;
			for (int i=0; i < DetectCommand.length - 1; i++)
			{
				if( i==0 )
				{
					sumxor = DetectCommand[0];					
				}else
				{
					sumxor = (byte) (sumxor^(DetectCommand[i]));
				}
			}
			DetectCommand[DetectCommand.length-1] = sumxor;
		}
		
		return DetectCommand;
	}
	
	//4F  06  00 00  09  02 F1 00  00
	public static byte[] retrieveSetDateTimeCommand(byte[] BCDDateTime)
	{
		if (BCDDateTime.length != 6)
		{
			return null;
		}
		if (DetectCommand == null)
		{
			DetectCommand = new byte[]{ 0x4F,0x06,(byte) 0x00,(byte) 0x00,0x09,
					(byte) 0x02,(byte) 0xF1,(byte)0x00,0x00,
					0x00,0x00,0x00,0x00,0x00,0x00, 0x00 };
			fillinSerialNumber(DetectCommand);
			for (int j=0; j<BCDDateTime.length; j++)
			{
				DetectCommand[j+9] = BCDDateTime[j];
			}
			byte sumxor = 0 ;
			for (int i=0; i < DetectCommand.length - 1; i++)
			{
				if( i==0 )
				{
					sumxor = DetectCommand[0];					
				}else
				{
					sumxor = (byte) (sumxor^(DetectCommand[i]));
				}
			}
			DetectCommand[DetectCommand.length-1] = sumxor;
		}
		
		return DetectCommand;
	}
	
	//4F  06  00 00  09  02 F1 FB 48
	public static byte[] retrieveVersionCommand()
	{
		
		if (DetectCommand == null)
		{
			DetectCommand = new byte[]{ 0x4F,0x06,(byte) 0x00,(byte) 0x00,0x03,
					(byte) 0x02,(byte) 0xF1,(byte)0xFB,0x00 };
			fillinSerialNumber(DetectCommand);
			
			byte sumxor = 0 ;
			for (int i=0; i < DetectCommand.length - 1; i++)
			{
				if( i==0 )
				{
					sumxor = DetectCommand[0];					
				}else
				{
					sumxor = (byte) (sumxor^(DetectCommand[i]));
				}
			}
			DetectCommand[DetectCommand.length-1] = sumxor;
		}
		
		return DetectCommand;
	}
	
	//4F  06  00 00  09  02 F1 FC 4F
	public static byte[] retrieveControlCommand()
	{
		
		if (DetectCommand == null)
		{
			DetectCommand = new byte[]{ 0x4F,0x06,(byte) 0x00,(byte) 0x00,0x03,
					(byte) 0x02,(byte) 0xF1,(byte)0xFC,0x00 };

			fillinSerialNumber(DetectCommand);
			byte sumxor = 0 ;
			for (int i=0; i < DetectCommand.length - 1; i++)
			{
				if( i==0 )
				{
					sumxor = DetectCommand[0];					
				}else
				{
					sumxor = (byte) (sumxor^(DetectCommand[i]));
				}
			}
			DetectCommand[DetectCommand.length-1] = sumxor;
		}
		
		return DetectCommand;
	}
	
	//4F  06  00 00  09  02 F1 00 05 00 01 B8 
	public static byte[] retrieveDataCommand(int num)
	{
		
		if (DetectCommand == null)
		{
			DetectCommand = new byte[]{ 0x4F,0x06,(byte) 0x00,(byte) 0x00,0x06,
					(byte) 0x02,(byte) 0xF1,(byte)0x00,0x05,
					0x00,0x00,0x00 };
			fillinSerialNumber(DetectCommand);
			int high = num / 256;
			int low = num - (256 * high);
			DetectCommand[9] = (byte) (0xFF & high);
			DetectCommand[10] = (byte) (0xFF & low);
			byte sumxor = 0 ;
			for (int i=0; i < DetectCommand.length - 1; i++)
			{
				if( i==0 )
				{
					sumxor = DetectCommand[0];					
				}else
				{
					sumxor = (byte) (sumxor^(DetectCommand[i]));
				}
			}
			DetectCommand[DetectCommand.length-1] = sumxor;
		}
		
		return DetectCommand;
	}
	
	//4F 06 00 00 06 02 F1 FF 00 43 
	public static byte[] retrieveOKCommand(int num)
	{
		
		if (DetectCommand == null)
		{
			DetectCommand = new byte[]{ 0x4F,0x06,(byte) 0x00,(byte) 0x00,0x06,
					(byte) 0x02,(byte) 0xF1,(byte)0xFF,0x00,0x00 };
			fillinSerialNumber(DetectCommand);
			int high = num % 256;
			int low = num - (256 * high);
			DetectCommand[9] = (byte) (0xFF & high);
			DetectCommand[10] = (byte) (0xFF & low);
			byte sumxor = 0 ;
			for (int i=0; i < DetectCommand.length - 1; i++)
			{
				if( i==0 )
				{
					sumxor = DetectCommand[0];					
				}else
				{
					sumxor = (byte) (sumxor^(DetectCommand[i]));
				}
			}
			DetectCommand[DetectCommand.length-1] = sumxor;
		}
		
		return DetectCommand;
	}
	
	//4F 06 00 00 06 02 F1 FF 01 42 
	public static byte[] retrieveErrorCommand(int num)
	{
		
		if (DetectCommand == null)
		{
			DetectCommand = new byte[]{ 0x4F,0x06,(byte) 0x00,(byte) 0x00,0x06,
					(byte) 0x02,(byte) 0xF1,(byte)0xFF,0x01,0x00 };
			fillinSerialNumber(DetectCommand);
			int high = num % 256;
			int low = num - (256 * high);
			DetectCommand[9] = (byte) (0xFF & high);
			DetectCommand[10] = (byte) (0xFF & low);
			byte sumxor = 0 ;
			for (int i=0; i < DetectCommand.length - 1; i++)
			{
				if( i==0 )
				{
					sumxor = DetectCommand[0];					
				}else
				{
					sumxor = (byte) (sumxor^(DetectCommand[i]));
				}
			}
			DetectCommand[DetectCommand.length-1] = sumxor;
		}
		
		return DetectCommand;
	}
}
